package com.weipu.common.facade.model;

import java.io.Serializable;

import com.weipu.common.util.GlobalConfig;

import android.os.Parcel;
import android.os.Parcelable;

public class ParcelableModel<T extends Serializable> implements Parcelable
{
    
    private T data;
    
    private ClassLoader classLoader;
    
    @SuppressWarnings("unchecked")
    public static Parcelable.Creator<ParcelableModel> CREATOR = new Creator<ParcelableModel>()
    {
        
        public ParcelableModel[] newArray(int size)
        {
            return new ParcelableModel[size];
        }
        
        public ParcelableModel createFromParcel(Parcel source)
        {
            return new ParcelableModel(source);
        }
    };
    
    public ParcelableModel(T content)
    {
        this.data = content;
        if (null != content)
        {
            classLoader = GlobalConfig.class.getClass().getClassLoader();
        }
    }
    
    @SuppressWarnings("unchecked")
    public ParcelableModel(Parcel source)
    {
        if (null != source)
        {
        	data = (T) source.readValue(GlobalConfig.class.getClass().getClassLoader());
        }
    }
    
    public int describeContents()
    {
        return 0;
    }
    
    public void writeToParcel(Parcel dest, int flags)
    {
        dest.writeValue(data);
    }
    
    public T getData()
    {
        return data;
    }
}
